﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Hook.Graphics;
using Hook.Graphics.SpriteSheets;
using Microsoft.Xna.Framework.Input;
using Hook.MenuAssets.Screens;

namespace Hook.MenuAssets
{
    public class ScreenManager
    {
        public Stack<Screen> ScreenStack;

        public static MainMenuScreen MainMenuScreen = new MainMenuScreen();
        public static GamePlayScreen GamePlayScreen = new GamePlayScreen();
        public static PropEditorScreen PropEditorScreen = new PropEditorScreen();
        public static LevelEditorScreen LevelEditorScreen = new LevelEditorScreen();
        public static LevelSelectScreen LevelSelectScreen = new LevelSelectScreen();
        public static BackScreen BackScreen = new BackScreen();

        public Sprite MouseSprite;

        public ScreenManager()
        {
            ScreenStack = new Stack<Screen>();
            MainMenuScreen.LinkButtons();
            GamePlayScreen.LinkButtons();
            PropEditorScreen.LinkButtons();
            LevelEditorScreen.LinkButtons();
            LevelSelectScreen.LinkButtons();

            MouseSprite = new Sprite(Game1.PrimitiveSheet, PrimitiveSheet.CURSOR);

            ScreenStack.Push(MainMenuScreen);
        }

        public void Update(GameTime gt)
        {
            Screen CurrentScreen = ScreenStack.Peek();

            if(CurrentScreen.NextScreen == BackScreen)
            {
                if(!CurrentScreen.IsTransOut)
                    CurrentScreen.CueExit();

                if(CurrentScreen.TransTimer == 0)
                {
                    CurrentScreen.IsTransOut = false;
                    ScreenStack.Pop();
                    //ScreenStack.Peek().CueEntrance();
                }
            }
            else if(CurrentScreen.NextScreen != null)
            {
                ScreenStack.Push(CurrentScreen.NextScreen);
                CurrentScreen.NextScreen = null;
                ScreenStack.Peek().CueEntrance();
            }

            CurrentScreen = ScreenStack.Peek();
            
            if(CurrentScreen is GamePlayScreen)
            {
                ((GamePlayScreen)CurrentScreen).Update(gt);
            }
            else if (CurrentScreen is PropEditorScreen)
            {
                ((PropEditorScreen)CurrentScreen).Update(gt);
            }
            else if (CurrentScreen is LevelEditorScreen)
            {
                ((LevelEditorScreen)CurrentScreen).Update(gt);
            }
            else
            {
                CurrentScreen.Update(gt);
            }

            MouseSprite.Position = new Vector3(Mouse.GetState().X - Game1.RESOLUTION.X / 2, Mouse.GetState().Y - Game1.RESOLUTION.Y / 2, 0);
            MouseSprite.Update(gt);
        }
        public void Draw(SpriteBatch sb)
        {
            Screen CurrentScreen = ScreenStack.Peek();
            
            if(CurrentScreen is GamePlayScreen)
            {
                ((GamePlayScreen)CurrentScreen).Draw(sb);
            }
            else if (CurrentScreen is PropEditorScreen)
            {
                ((PropEditorScreen)CurrentScreen).Draw(sb);
            }
            else if (CurrentScreen is LevelEditorScreen)
            {
                ((LevelEditorScreen)CurrentScreen).Draw(sb);
            }
            else
            {
                CurrentScreen.Draw(sb);
            }
            MouseSprite.Draw(sb);
        }
    }
}
